import common_appProxy from '@/common/business/common_appProxy.js'
import common_const from '@/common/business/common_const.js'
import common_emit from '@/common/business/common_emit.js'
import common_msg from '@/common/business/common_msg'
import common_api from '@/common/business/common_api'
import common_cache from '@/common/business/common_cache'
import common_jump from '@/common/business/common_jump'
import common_cloude from '@/common/business/common_cloude'
import common_wxjsapi from '@/common/business/common_wxjsapi'


// 职责:所有业务公共相关的代码放这里
const bs = {
	validatePhone() {
		if (!getApp().globalData.userInfo.wxUserInfoMobilePhone) {
			const url = getApp().globalData.VuePrototype.$util.getCurrentPageUrlAndArgs();
			this.goLogin(url);
			return;
		}else {
			return true;
		}
	},
	setVuex(name, value) {
		getApp().globalData.VuePrototype.$u.vuex(name, value);
	},
	// 调用上个页面的方法
	callPrePageMethod(methodName, data, index = 2) {
		const pages = getCurrentPages(); //当前页
		const beforePage = pages[pages.length - index]; //上个页面

		// #ifdef H5
		beforePage[methodName](data);
		// #endif
		// #ifndef H5
		beforePage.$vm[methodName](data);
		// #endif
	},
	// 设置分享GUID
	setFromUserGUID(fromUserGUID, globalData = getApp().globalData) {
		if (!fromUserGUID) {
			const queryObj = globalData.VuePrototype.$util.getQueryObjH5();
			if (queryObj.fromUserGUID) {
				fromUserGUID = queryObj.fromUserGUID
			}
		}

		if (fromUserGUID) {
			this.setVuex('vuex_fromUserGUID', fromUserGUID);
		}
	},
	// 获取分享用户信息的url
	getShareUserInfoUrl(id) {
		let url = `${getApp().globalData.VuePrototype.$window.origin}/pages/40mySub/userInfo/userInfo?id=${id}`;
		if (getApp().globalData.userInfo) {
			if (!url.includes('fromUserGUID')) {
				url += `&fromUserGUID=${getApp().globalData.userInfo.id}`;
			}
		}
		return url;
	},
	// 获取分享活动
	getShareActiveUrl(id) {
		let url = `${getApp().globalData.VuePrototype.$window.origin}/pages/20searchSub/hdDetail/hdDetail?id=${id}`;
		if (getApp().globalData.userInfo) {
			if (!url.includes('fromUserGUID')) {
				url += `&fromUserGUID=${getApp().globalData.userInfo.id}`;
			}
		}
		return url;
	},

	// 添加微信
	addWx(activeContractWxUrl) {
		activeContractWxUrl = activeContractWxUrl || 'https://pic.cwyyt.cn/upload/00whtc/addwx.jpg';
		getApp().globalData.VuePrototype.$util.showImg([
			`${activeContractWxUrl}?rnd=${new Date().getTime()}`
		]);
	},
	// 添加微信群
	addWxGroup() {
		getApp().globalData.VuePrototype.$util.showImg([
			`https://pic.cwyyt.cn/upload/00whtc/addwxgroup.jpg?rnd=${new Date().getTime()}`
		]);
	},

	// 初始化数据
	initData(globalData) {
		const location = globalData.VuePrototype.$window.location;
		// 用户信息处理
		let userInfo = globalData.userInfo;
		if(!userInfo&&location.hostname=="localhost"){
			userInfo={
				id: "b4a092d8-9f66-47be-4199-08db9bf795c1",
				openid: "oIaV0wzU3owQpNB2JipxTErO4nlc"
			};
			globalData.VuePrototype.$storage.setAppUserInfo(userInfo);
		}
		
		this.addInterceptor(globalData); // 加入登录处理
		this.setFromUserGUID(null,globalData); // 设置谁分享的
		
		if (userInfo) {
			this.refreshUserInfo(userInfo.id, globalData);
			// this.saveUserInfo({
			// 	id:userInfo.id,
			// 	lastOpenTime: globalData.VuePrototype.$util.getNow()
			// },false);
		} else {
			// 检查当前页面
			this.checkLogin(location.pathname + location.search);
		}
	},

	// 退出登录
	async loginOut(isShowConfirm = true) {
		let res = 1;
		if (isShowConfirm) {
			res = await this.showConfirm('确定退出系统吗?');
		}
		if (res) {
			uni.reLaunch({
				url: '/pages/_common/login/login'
			});
		}
	},
}

export default Object.assign(bs, common_appProxy, common_const, common_emit, common_msg, common_api, common_cache,
	common_jump, common_cloude, common_wxjsapi);